﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
//using AForge.Video;
//using AForge.Video.DirectShow;
using Microsoft.Win32;
using SkiaSharp;

namespace WpfUsercontrol.UserControls.ImageUpload
{
    /// <summary>
    /// ImageUpload.xaml 的交互逻辑
    /// </summary>
    public partial class ImageUpload : UserControl
    {
        //private FilterInfoCollection videoDevices;
        //private VideoCaptureDevice videoSource;
        private bool isCameraRunning = false;

        public event Action<BitmapImage> PhotoCaptured;
        public ImageUpload()
        {
            InitializeComponent();
            Loaded += OnLoaded;
            Unloaded += OnUnloaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            // 获取所有视频设备
            //videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        }

        private void OnUnloaded(object sender, RoutedEventArgs e)
        {
            StopCamera();
        }

       

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            //if (videoDevices == null || videoDevices.Count == 0)
            //{
            //    MessageBox.Show("没有找到可用的摄像头设备");
            //    return;
            //}

            //// 使用第一个摄像头设备
            //videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            //videoSource.NewFrame += VideoSource_NewFrame;
            //videoSource.Start();

            isCameraRunning = true;
            btnUpload.IsEnabled = false;
            btnSure.IsEnabled = true;
        }

        private void btnSure_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

        }


        //private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        //{
        //    try
        //    {
        //        using (var bitmap = (Bitmap)eventArgs.Frame.Clone())
        //        {
        //            // 将Bitmap转换为BitmapSource以便在WPF中显示
        //            var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
        //                bitmap.GetHbitmap(),
        //                IntPtr.Zero,
        //                Int32Rect.Empty,
        //                BitmapSizeOptions.FromEmptyOptions());

        //            // 必须在UI线程上更新Image控件
        //            Dispatcher.Invoke(() =>
        //            {
        //                CameraView.Source = bitmapSource;
        //            });
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Dispatcher.Invoke(() => MessageBox.Show($"摄像头错误: {ex.Message}"));
        //    }
        //}

        private void StopCamera()
        {
            //if (isCameraRunning && videoSource != null && videoSource.IsRunning)
            //{
            //    videoSource.SignalToStop();
            //    videoSource.WaitForStop();
            //    videoSource.NewFrame -= VideoSource_NewFrame;
            //    videoSource = null;
            //}

            //isCameraRunning = false;
            //CameraView.Source = null;
            //btnUpload.IsEnabled = true;
            //btnSure.IsEnabled = false;
            ////StopButton.IsEnabled = false;
        }
    }
}
